using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UniversalAdmin.Application.Dtos;
using UniversalAdmin.Application.Services;
using UniversalAdmin.Application.Dtos.User;
using System.Security.Claims;

namespace UniversalAdmin.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
    private readonly IAuthService _authService;
    
    public AuthController(IAuthService authService)
    {
        _authService = authService;
    }

    [HttpPost("login")]
    public async Task<IActionResult> Login(UsersDto request)
    {
        var response = await _authService.LoginAsync(request.Username, request.Password);
        
        if (response.Code == 200)
        {
            return Ok(response);
        }
        else
        {
            return BadRequest(response);
        }
    }

    [HttpPost("register")]
    public async Task<IActionResult> Register(UsersDto dto)
    {
        try
        {
            await _authService.RegisterAsync(dto);
            return Ok(new
            {
                code = 200,
                msg = "注册成功，请登录"
            });
        }
        catch (Exception ex)
        {
            return BadRequest(new
            {
                code = 400,
                msg = ex.Message
            });
        }
    }
}